﻿/* ============================================================================== 
 * 功能描述：StateMachine 
 * 创 建 者：Administrator
 * 创建日期：2016/4/26 13:05:51 
 * ==============================================================================*/

using System;
using System.Collections.Generic;
using System.Security.Policy;
using UnityEngine;

public enum Enum_StateType
{
    MoveTo=1,
}

public class StateMachine
{
    /// <summary>
    /// 所有状态集合
    /// </summary>
    private Dictionary<int,IState> stateDic=null;

    /// <summary>
    /// 当前状态
    /// </summary>
    private IState currentState;

    /// <summary>
    /// 状态改变委托
    /// </summary>
    /// <param name="oldState">旧状态</param>
    /// <param name="newState">新状态</param>
    /// <param name="parme"></param>
    public delegate void OnStateChangedDe(IState oldState,IState newState,params object[] parme);

    public OnStateChangedDe OnStateChangedCallBack;

    public StateMachine()
    {
        stateDic=new Dictionary<int, IState>();
        currentState = null;
        OnStateChangedCallBack = null;
    }

    /// <summary>
    /// 根据状态id从集合中获取状态
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    private IState GetStateById(int id)
    {
        if (stateDic.ContainsKey(id))
        {
            return stateDic[id];
        }
        return null;
    }

    /// <summary>
    /// 注册状态
    /// </summary>
    /// <param name="state"></param>
    public void RegisterState(IState state)
    {
        if(state==null)return;

        int id = state.GetStateId();
        if(stateDic.ContainsKey(id))return;

        stateDic.Add(id,state);
    }

    /// <summary>
    /// 注销状态
    /// </summary>
    /// <param name="id">状态id</param>
    public void UnRegisterState(int id)
    {
        if(!stateDic.ContainsKey(id))return;
        stateDic.Remove(id);
    }

    /// <summary>
    /// 注销状态
    /// </summary>
    /// <param name="state">状态</param>
    public void UnRegisterState(IState state)
    {
        if(state==null)return;
        UnRegisterState(state.GetStateId());
    }

    /// <summary>
    /// 停止当前状态
    /// </summary>
    public void StopState()
    {
        if(currentState==null)return;
        currentState.OnLeave(null);
    }

    /// <summary>
    /// 切换状态
    /// </summary>
    /// <param name="newStateId">新状态id</param>
    /// <param name="parme"></param>
    /// <returns></returns>
    public bool SwitchState(int newStateId, params object[] parme)
    {
        //如果新状态id==当前状态，则返回
        if (newStateId == currentState.GetStateId()) return false;

        IState state = GetStateById(newStateId);
        if(state==null)return false;

        if (currentState != null)
        {
            currentState.OnLeave(state, parme);
        }
        IState oldState = currentState;
        currentState = state;
        if (OnStateChangedCallBack != null)
        {
            OnStateChangedCallBack(oldState, currentState, parme);
        } 
        currentState.OnEnter(this, oldState, parme);
        return true;
    }

    /// <summary>
    /// 执行当前状态的执行方法
    /// </summary>
    public void OnUseState()
    {
        currentState.OnUse();
    }
}

public class MoveToState : IState
{
    public int GetStateId()
    {
        return (int) Enum_StateType.MoveTo;
    }

    public void OnEnter(StateMachine machine, IState oldState, params object[] param)
    {
    }

    public void OnUse()
    {

    }

    public void OnLeave(IState newState, params object[] param)
    {
    }
}
